package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpSocRelatedAddDTO;
import org.jsola.hr.dto.EmpSocRelatedUpdateDTO;
import org.jsola.hr.dto.excel.EmpImportSocRelatedDTO;
import org.jsola.hr.query.EmpSocRelatedQuery;
import org.jsola.hr.service.IEmpSocRelatedService;
import org.jsola.hr.vo.EmpSocRelatedListVO;
import org.jsola.hr.vo.EmpSocRelatedVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.validation.Valid;


/**
 * 员工社保相关信息表
 *
 * @author zhr
 */
@RestController("hrEmpSocRelatedController")
@RequestMapping(HrConstants.API_PATH + "/v1/empSocRelateds")
public class EmpSocRelatedController implements IEmpSocRelatedControllerAPI {

    @Autowired
    private IEmpSocRelatedService empSocRelatedService;

    @Override
    @PostMapping
    public Result<EmpSocRelatedVO> save(@Valid @RequestBody EmpSocRelatedAddDTO empSocRelatedAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工社保相关信息表权限校验

        return Result.success(
                empSocRelatedService.save(empSocRelatedAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpSocRelatedUpdateDTO empSocRelatedUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工社保相关信息表权限校验

        int count = empSocRelatedService.update(empSocRelatedUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{empSocRelatedId}")
    public Result<Integer> delete(@PathVariable String empSocRelatedId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工社保相关信息表权限校验

        int count = empSocRelatedService.deleteByIds(tokenUser, empSocRelatedId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empSocRelatedIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工社保相关信息表权限校验

        int count = empSocRelatedService.deleteByIds(tokenUser, empSocRelatedIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empSocRelatedId}")
    public Result<EmpSocRelatedVO> findById(@PathVariable String empSocRelatedId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工社保相关信息表权限校验

        return Result.success(
                empSocRelatedService.selectById(empSocRelatedId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpSocRelatedListVO>> listAll(@Valid EmpSocRelatedQuery empSocRelatedQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工社保相关信息表权限校验

        return Result.success(
                empSocRelatedService.select(empSocRelatedQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpSocRelatedListVO>> page(@Valid EmpSocRelatedQuery empSocRelatedQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                empSocRelatedService.selectPage(empSocRelatedQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping(value = "/initAllRelatedByCompany/{companyId}")
    public Result<String> initAllRelatedByCompany(@PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        empSocRelatedService.initAllRelatedByCompany(companyId, tokenUser);
        return Result.success();
    }

    @Override
    @GetMapping(value = "/findEmpSocRelated")
    public Result<EmpImportSocRelatedDTO> findEmpSocRelated(@RequestParam("empId") String empId,
                                                            @RequestParam("companyId") String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                empSocRelatedService.findEmpSocRelated(empId, companyId, tokenUser)
        );
    }

    @Override
    @PutMapping(value = "/empUpdateSocDate")
    public Result<String> empUpdateSocDate(@RequestBody EmpImportSocRelatedDTO updateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        empSocRelatedService.empUpdateSocDate(updateDTO, tokenUser);
        return Result.success();
    }

    @Override
    @PostMapping(value = "/empSocRelatedImportData")
    public Result<EmpSocRelatedListVO> empSocRelatedImportData(@RequestBody List<String> empList) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                empSocRelatedService.empSocRelatedImportData(empList, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/checkEmpCanBeDeleted/{empId}")
    public Result<Boolean> checkEmpCanBeDeleted(@PathVariable String empId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                empSocRelatedService.checkEmpCanBeDeleted(empId, tokenUser)
        );
    }

}

